package com.aqgj.util;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;

public class JsonWrapper {
    private static Logger loger = Logger.getLogger(JsonWrapper.class);

    public static HashMap<String, Object> successWrapper(Object pojo) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(true));
        wrapper.put("data", pojo);
        return wrapper;
    }

    public static HashMap<String, Object> failureWrapper(Object pojo) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(false));
        wrapper.put("data", pojo);
        return wrapper;
    }

    public static HashMap<String, Object> failureWrapperMsg(String msg) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(false));
        wrapper.put("msg", msg);
        return wrapper;
    }

    public static HashMap<String, Object> failureWrapper(Object... pojo) {
        return wrapper(false, pojo);
    }

    public static HashMap<String, Object> successWrapper(Object... pojo) {
        return wrapper(true, pojo);
    }

    public static HashMap<String, Object> wrapper(boolean success, Object... pojo) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(success));
        int length = pojo.length;
        if (pojo.length % 2 == 1) {
            length--;
        }
        length /= 2;
        for (int i = 0; i < length; i++) {
            wrapper.put(String.valueOf(pojo[(i * 2)]), pojo[(i * 2 + 1)]);
        }
        return wrapper;
    }

    public static HashMap<String, Object> wrapperMap(boolean success, Map<String, Object> map) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(success));
        wrapper.put("data", map);
        return wrapper;
    }

    public static Map<String, Object> wrapperDataRows(List<Object> dataRows) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(true));
        wrapper.put("Rows", dataRows);
        return wrapper;
    }

    public static String toJsonString(Object jsonObj)
            throws Exception {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return mapper.writeValueAsString(jsonObj).toString();
        } catch (Exception e) {
            loger.error("将对象序列化为JSON String时发送错误:" + e.getMessage());
            throw new Exception("将对象序列化为JSON String时发送错误,无法转换.");
        }
    }

    public static HashMap<String, Object> successPagerWrapper(Object pojo1,Integer countTotal) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(true));
        wrapper.put("data", pojo1);
        wrapper.put("countTotal",countTotal);
        return wrapper;
    }

    public static HashMap<String, Object> successPagerWrapper(Object pojo1,Integer countTotal,Object value) {
        HashMap<String, Object> wrapper = new HashMap();
        wrapper.put("success", Boolean.valueOf(true));
        wrapper.put("data", pojo1);
        wrapper.put("countTotal",countTotal);
        wrapper.put("sumValue",value);
        return wrapper;
    }
}
